import {
    Entity,
    PrimaryGeneratedColumn,
    Column,
    CreateDateColumn,
    UpdateDateColumn,
    OneToMany,
    Index
} from 'typeorm';
import { KycSubmission } from '../../kyc/entities/kyc-submission.entity';
import { Application } from '../../applications/entities/application.entity';
import { OtcOrder } from '../../otc/entities/otc-order.entity';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';

export type UserRole = 'user' | 'admin' | 'operator';

@Entity('users')
@Index(['walletAddress', 'chainId'], { unique: true })
export class User {
    @ApiProperty({
        description: 'Ethereum wallet address as primary identifier',
        example: '0x742d35Cc6634C0532925a3b8D4C9db96C4b4d8b6',
        minLength: 42,
        maxLength: 42
    })
    @PrimaryGeneratedColumn()
    id: number;

    @ApiProperty({
        description: 'Ethereum wallet address as primary identifier',
        example: '0x742d35Cc6634C0532925a3b8D4C9db96C4b4d8b6',
        minLength: 42,
        maxLength: 42
    })
    @Column({ length: 42 })
    @Index()
    walletAddress: string;

    @ApiProperty({
        description: 'Blockchain chain ID',
        example: 1,
        minimum: 1
    })
    @Column({ type: 'int' })
    @Index()
    chainId: number;

    @ApiPropertyOptional({
        description: 'User\'s full name',
        example: 'John Doe',
        required: false
    })
    @Column({ nullable: true, length: 100 })
    fullName?: string;

    @Column({ type: 'enum', enum: ['user', 'admin', 'operator'], default: 'user' })
    role: UserRole;

    @Column({ nullable: true })
    email?: string;

    @Column({ default: true })
    isActive: boolean;

    @ApiProperty({
        description: 'User creation timestamp',
        example: '2024-01-01T00:00:00.000Z'
    })
    @CreateDateColumn()
    createdAt: Date;

    @ApiProperty({
        description: 'User last update timestamp',
        example: '2024-01-01T00:00:00.000Z'
    })
    @UpdateDateColumn()
    updatedAt: Date;

    // 关联关系
    @OneToMany(() => KycSubmission, (kyc) => kyc.user)
    kycSubmissions: KycSubmission[];

    @OneToMany(() => Application, (app) => app.user)
    applications: Application[];

    @OneToMany(() => OtcOrder, (order) => order.user)
    orders: OtcOrder[];

}
